<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="addHandle">{{ $t('新增') }}</el-button>
      <el-button size="mini" type="danger" :disabled="btnDis" icon="el-icon-minus" @click="deleteHandle">删除</el-button>
    </el-row>
    <el-table
      v-loading="loading"
      :data="onlCgformHeadBtnList"
      tooltip-effect="darkTable"
      :row-style="{height: '50px'}"
      :cell-style="{padding: '3px'}"
      @row-click="editRow"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" align="center" width="55" />
      <el-table-column :label="$t('按钮名称')" align="center" prop="btnName" />
      <el-table-column :label="$t('按钮编码')" align="center" prop="btnCode" />
      <el-table-column :label="$t('按钮图标')" align="center" prop="btnIcon" />
      <el-table-column :label="$t('按钮样式')" align="center" prop="btnClassIfy" />
      <el-table-column :label="$t('按钮位置')" align="center" prop="btnPosition" />
    </el-table>
    <!-- 添加或修改自定义按钮配置对话框 -->
    <el-dialog
      :title="$t(title)"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="dialogForm"
      append-to-body
    >
      <btnForm ref="btnForm" />
    </el-dialog>
  </div>
</template>
<script>
import { delOnlCgformHeadBtn, getListByFormId } from '@/api/system/desform/data/onlCgformHeadBtn'
import btnForm from '@/components/desform/head/btnForm'
import { mapState } from 'vuex'

export default {
  components: {
    btnForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 自定义按钮配置表格数据
      onlCgformHeadBtnList: [],
      // 弹出层标题
      title: '自定义按钮',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        btnName: undefined,
        btnCode: undefined,
        btnIcon: undefined,
        btnClassIfy: undefined,
        btnPosition: undefined,
        btnDisplay: undefined,
        btnHandlerMode: undefined,
        headId: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      headId: '',
      multipleTable: [],
      btnDis: true
    }
  },
  computed: {
    ...mapState('dbAbout', ['head'])
  },
  created() {
    if (this.head) {
      this.getList()
    }
  },
  methods: {
    /** 查询自定义按钮配置列表 */
    getList() {
      this.loading = true
      getListByFormId(this.head.id).then(response => {
        this.onlCgformHeadBtnList = response.data
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    init(row) {
      if (row) {
        this.headId = row.id
        this.getList()
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        btnName: undefined,
        btnCode: undefined,
        btnIcon: undefined,
        btnClassIfy: undefined,
        btnPosition: undefined,
        btnDisplay: undefined,
        btnHandlerMode: undefined,
        headId: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.btnForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.btnForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle() {
      if (this.multipleTable.length === 1) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        return delOnlCgformHeadBtn(this.multipleTable[0].id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
        this.loading = false
      })
      } else {
          this.$message({ message: '至多选择一条数据!', type: 'warning' })
      }
    },
    editRow(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.btnForm.init(row)
      })
    },
    selectionChangeHandle(val) {
       this.multipleTable = val
       this.btnDis = !(val.length > 0)
    }
  }
}
</script>
